package com.vhuomai.product.home.controller;

import com.quanyue.auto.util.ServiceResult;
import com.quanyue.auto.web.Scope;
import com.vhuomai.product.home.entity.BaseHomeCategory;
import com.vhuomai.product.home.service.HomeCategoryService;
import com.vhuomai.system.constant.UserRole;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Cui
 */
@Api(tags = "首页产品目录管理")
@RestController
@RequestMapping("/api")
public class HomeCategoryController {
    @Autowired
    private HomeCategoryService homeCategoryService;

    @ApiOperation(value = "添加目录")
    @Scope(role = UserRole.MANAGER)
    @PostMapping(value = "/home/product/category", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<Boolean> addHomeCategory(@RequestBody BaseHomeCategory homeCategory) {
        homeCategoryService.createHomeCategory(homeCategory);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "修改目录")
    @Scope(role = UserRole.MANAGER)
    @PutMapping(value = "/home/product/category", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<Boolean> updateHomeCategory(@RequestBody BaseHomeCategory homeCategory) {
        homeCategoryService.updateHomeCategory(homeCategory);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "删除目录")
    @Scope(role = UserRole.MANAGER)
    @DeleteMapping(value = "/home/product/category", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<Boolean> deleteHomeCategory(@RequestParam("id") Integer id) {
        homeCategoryService.deleteHomeCategory(id);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "目录列表")
    @GetMapping(value = "/home/product/category", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<List<BaseHomeCategory>> homeCategoryList() {
        return ServiceResult.success(homeCategoryService.findHomeCategoryList());
    }
}
